package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DeleteOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍");
        System.out.println("请输入你要删除的书籍");
        Scanner scan = new Scanner(System.in);
        String name = scan.nextLine();
        int i =0;
        int index =0;
        for(i =0;i<bookList.getUsedSize();++i) {
            Book book = bookList.getPos(i);
            if(bookList.getPos(i).bookName.equals(name)) {
                System.out.println("查找图书成功~~~" + "~~~图书信息如下：");
                System.out.println(book);
                index =i;
                break;
            }
        }
        //跳出来有两种情况:
        //2.一种是全部遍历完成
        if(i==bookList.getUsedSize()){
            System.out.println("没有找到这本书,无法删除");
            return;
        }
        //1.一种是找到要删除的书籍
         for(int j = index;j< bookList.getUsedSize()-1;++j) {
             Book book = bookList.getPos(j+1);//获取这本书
             bookList.setBook(j,book);
             System.out.println("删除图书成功~~~");
         }
         bookList.setUsedSize(bookList.getUsedSize()-1);
         return;
    }
}
